// Copyright (c) Microsoft Corporation.  All rights reserved.
#include "stdafx.h"
#include "AutoButton.h"

BEGIN_MESSAGE_MAP(CAutoButton, CButton)
    ON_WM_TIMER()
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

const UINT_PTR CAutoButton::m_timerID = 0xe01;
const UINT CAutoButton::m_timerElapse = 150;    // the button event interval is 200 ms 
const UINT CAutoButton::m_timerSkip = 3;    // skip 3 timers for first button event

CAutoButton::CAutoButton(void)
{
}

CAutoButton::~CAutoButton(void)
{
}

void CAutoButton::OnLButtonDown(UINT nFlags, CPoint point)
{
    m_timerCount = 0;
    SetTimer(m_timerID, m_timerElapse, 0);
    CWnd::OnLButtonDown(nFlags, point);
}

void CAutoButton::OnLButtonUp(UINT nFlags, CPoint point)
{
    KillTimer(m_timerID);
    CWnd::OnLButtonUp(nFlags, point);
}

void CAutoButton::OnTimer(UINT_PTR nIDEvent)
{
    if (nIDEvent != m_timerID)
    {
        CWnd::OnTimer(nIDEvent);
        return;
    }

    m_timerCount++;

    if (m_timerCount <= m_timerSkip)
        return;

    // sendout button click event to parent
    CWnd* pParent = GetParent();

    pParent->SendMessage(WM_COMMAND,
            MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED),
            (LPARAM)m_hWnd);
}
